package com.example.bingobox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.bingobox.R;
import com.example.bingobox.model.State;
import com.example.bingobox.model.State.LocationMark;

import java.util.ArrayList;
import java.util.List;

public class LocationMarkRecyclerAdapter extends RecyclerView.Adapter<LocationMarkRecyclerAdapter.ViewHolder> {
    private final Context context;
    private final State state;
    private final List<LocationMark> marks;

    public LocationMarkRecyclerAdapter(Context context, State state) {
        this.context = context;
        this.state = state;
        this.marks = new ArrayList<>();
        // 收集所有标记
        for (List<LocationMark> cityMarks : state.getCityMarks().values()) {
            marks.addAll(cityMarks);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_location_mark, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LocationMark mark = marks.get(position);
        holder.locationName.setText(mark.getLocation().getName());
        holder.locationType.setText(mark.getType());
    }

    @Override
    public int getItemCount() {
        return marks.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        final TextView locationName;
        final TextView locationType;

        ViewHolder(View view) {
            super(view);
            locationName = view.findViewById(R.id.location_text);
            locationType = view.findViewById(R.id.location_type);
        }
    }
} 